package com.heitaoking.blog.servlet;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.heitaoking.blog.dao.BlogDao;
import com.heitaoking.blog.entity.Blog;
import com.heitaoking.blog.entity.User;

/**
 * 博文登录Servlet类
 * 
 * @author getan
 * 
 */
public class FabowenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 博文Dao对象
	BlogDao dao = null;

	/**
	 * 初始化方法
	 * 
	 * 将初始化dao对象的操作放入init()方法,可保证dao只被创建一次
	 */
	@Override
	public void init() throws ServletException {
		// 初始化博文Dao对象
		dao = new BlogDao();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置请求编码集为utf-8(避免中文乱码)
		request.setCharacterEncoding("utf-8");
		// 创建博文数据视图对象(用于封装新增博文信息的数据)
		Blog blog= new Blog();
		request.getParameter("biaotiname");
		request.getParameter("biaotiname2");
		
		System.out.println(request.getParameter("biaotiname"));
		System.out.println(request.getParameter("biaotiname2"));
		

		// 从页面获取新增数据
		blog.setBiaotiname(request.getParameter("biaotiname"));
		blog.setBiaotiname2(request.getParameter("biaotiname2"));
		
		// 从request对象获得session(会话)对象
		HttpSession session = request.getSession();
		
		User currentUser = (User)session.getAttribute("currentUser");
		
		blog.setUsid(currentUser.getUsid());
		
		try {
			// 调用dao中的新增博文信息方法
			dao.insertBlog(blog);
			
			// 将新增结果保存到request对象中(该提示信息会显示在JSP页面中的 <div id='msgs'>${messages}</div>处)
			request.setAttribute("blog", "博文注册成功!");
			System.out.println("ggggg");
			// 调用查询博文列表的Servlet,再次显示新增该博文后的博文信息列表
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/yemianJSP/DH-bowen guanli.jsp");
			// 请求转发给博文信息列表Servlet(/userQuery)
			rd.forward(request, response);
			
		} catch (Exception e) {
//			// 将新增结果保存到request对象中(该提示信息会显示在JSP页面中的 <div id='msgs'>${messages}</div>处)
     //		request.setAttribute("messages", "博文注册失败!");
//			
//			// 调用查询博文列表的Servlet,再次显示新增该博文后的博文信息列表
		//	RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/yemianJSP/DH-fabowen.jsp");
//			// 请求转发给博文信息列表Servlet(/userQuery)
		//	rd.forward(request, response);
//			
			
			// 控制台输出异常信息
						e.printStackTrace();
						
						// 将新增结果保存到request对象中(该提示信息会显示在JSP页面中的 <div id='msgs'>${messages}</div>处)
						request.setAttribute("messages", "编写博文失败!");
						
						// 返回新增学生信息页面，重新填写学生信息
					// rd = getServletContext().getRequestDispatcher("/jsp/yemianJSP/DH-fabowen.jsp");
						// 请求转发给新增学生信息页面(/jsp/student/studentAdd.jsp)
				//		rd.forward(request, response);
						

						// 返回新增学生信息页面，重新填写学生信息
						RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/yemianJSP/DH-fabowen.jsp");
						// 请求转发给新增学生信息页面(/jsp/student/studentAdd.jsp)
						rd.forward(request, response);
		}
	}
}